﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace SnmpForm.ApSearch
{
    class IPAddressExtend
    {
        static public IPAddress ZeroAddress = 
            new IPAddress(new byte[] { 0, 0, 0, 0 }); 
        static public uint IPAddressToLong(string IPAddr)
        {
            System.Net.IPAddress oIP = System.Net.IPAddress.Parse(IPAddr);
            byte[] byteIP = oIP.GetAddressBytes();
            uint ip = (uint)byteIP[3] << 24;
            ip += (uint)byteIP[2] << 16;
            ip += (uint)byteIP[1] << 8;
            ip += (uint)byteIP[0];
            return ip;
        }
        static public uint IPAddressToLongBackwards(string IPAddr)
        {
            System.Net.IPAddress oIP = System.Net.IPAddress.Parse(IPAddr);
            byte[] byteIP = oIP.GetAddressBytes();
            uint ip = (uint)byteIP[0] << 24;
            ip += (uint)byteIP[1] << 16;
            ip += (uint)byteIP[2] << 8;
            ip += (uint)byteIP[3];
            return ip;
        }

        static public bool LessOrEquals(IPAddress a, IPAddress b)
        {
            long nA = IPAddressToLongBackwards(a.ToString());
            long nB = IPAddressToLongBackwards(b.ToString());
            return nA <= nB;
        }
        static public IPAddress IPAdd(IPAddress ipaddr)
        {
            byte[] bytes = ipaddr.GetAddressBytes();
            if (++bytes[3] > 254)
            {
                bytes[2]++;
                bytes[3] = 1;
            }
            if (bytes[2] > 254)
            {
                bytes[1]++;
                bytes[2] = 1;
            }
            if (bytes[1] > 254)
            {
                bytes[0]++;
                bytes[1] = 1;
            }
            if (bytes[0] > 254)
            {
                bytes[0] = 1;
            }

            IPAddress addr = new IPAddress(bytes);
            return addr;
        }

        static public long IpInterval(IPAddress a, IPAddress b)
        {
            long nInterval = 0;
            long nA = IPAddressToLongBackwards(a.ToString());
            long nB = IPAddressToLongBackwards(b.ToString());
            if (nA > nB)
            {
                nInterval = nA - nB;
            }
            else
            {
                nInterval = nB - nA;
            }
            return nInterval;
        }

        static public long IpInterval(string a, string b)
        {
            IPAddress addrA = IPAddress.Parse(a);
            IPAddress addrB = IPAddress.Parse(b);
            return IpInterval(addrA, addrB);
        }

        static public byte[] IpByteByStr(string strIp)
        {
            try
            {
                System.Net.IPAddress a = System.Net.IPAddress.Parse(strIp);
                return a.GetAddressBytes();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }
        static public string IpStrByByte(byte[] ip)
        {
            System.Net.IPAddress a = new System.Net.IPAddress(ip);
            return a.ToString();
        }
    }
}
